package cn.springcloud.provider.controller;


import cn.springcloud.api.apiUtils.POIUtil;
import cn.springcloud.provider.service.UserService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("excelExport")
public class ExcelExportController {

    @Autowired
    private UserService userService ;

    /**
     * 导出用户信息Excel
     * @return
     */
    @GetMapping
    public Object exportUserExcel(HttpServletRequest request, HttpServletResponse response){
        //获取数据
        List<Map<String,Object>> resultList = userService.findAllUser();
        //excel标题
        String[] title = {"id","userName","dbSource","phone","pwd","email"}; //sheet名
        String sheetName = "用户信息表";
        //创建HSSFWorkbook(使用list)
        //HSSFWorkbook wb = POIUtil.getHSSFWorkbook(sheetName, title, resultList, null);

        String[][] content = new  String[resultList.size()][6];
        for (int i = 0; i < resultList.size(); i++) {
            Map<String, Object> obj = resultList.get(i);
            content[i][0] = String.valueOf(obj.get("id"));
            content[i][1] = String.valueOf(obj.get("userName"));
            content[i][2] = String.valueOf(obj.get("dbSource"));
            content[i][3] = String.valueOf(obj.get("phone"));
            content[i][4] = String.valueOf(obj.get("pwd"));
            content[i][5] = String.valueOf(obj.get("email"));
        }
        //创建HSSFWorkbook(使用数组)
        HSSFWorkbook wb = POIUtil.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            //excel文件名
            String fileName = sheetName+"_"+System.currentTimeMillis()+".xls";
            //设置相应流格式信息等
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            //将excel对象写到相应流中
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null ;
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
